package com.kzd.btgame.main.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.IncludeAppRefreshBinding
import com.kzd.btgame.entity.GameBean
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.kzd.btgame.main.game.adapter.GameBargainAdapter
import com.kzd.btgame.main.mine.adapter.MallCollectAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MineCollectFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>() {

    companion object {
        const val KEY_COLLECT_ACTION = "KEY_COLLECT_ACTION"
        fun newInstance(action: Int): MineCollectFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_ACTION, action)
            }
            return MineCollectFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mGame1Adapter = Game1Adapter<GameBean>()

    private val mGameBargainAdapter = GameBargainAdapter()

    private val mMallCollectAdapter = MallCollectAdapter()

    private val mAction: Int by lazy {
        arguments?.getInt(KEY_COLLECT_ACTION) ?: 0
    }

    private val mViewModel:MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {

        mViewBinding.rvAppRefreshBody.apply {
            layoutManager = if (mAction == 2) {
                GridLayoutManager(context, 2)
            } else {
                LinearLayoutManager(context)
            }
        }.adapter = when (mAction) {
            1 -> mGameBargainAdapter
            2 -> mMallCollectAdapter
            else -> mGame1Adapter
        }

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            initData(isLoadMore = false)
        }
        mViewBinding.mSmartRefreshLayout.setOnLoadMoreListener {
            initData(isLoadMore = true)
        }
    }

    override fun initData() {
        initData(isLoadMore = false)
    }

    fun initData(isLoadMore: Boolean = false) {
        when (mAction) {
            1 -> mViewModel.mineCollectBargain(isLoadMore)
            2 -> mViewModel.mineCollectGoods(isLoadMore)
            else -> mViewModel.mineCollectGame(isLoadMore)
        }
    }

    override fun onRefreshLoad() {
        super.onRefreshLoad()
        initData(isLoadMore = false)
    }

    override fun initViewMode() {

        mViewModel.collectGameLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mGame1Adapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mGame1Adapter.addData(data)
            } else {
                mGame1Adapter.setNewInstance(data)
            }
        })

        mViewModel.collectBargainLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mGameBargainAdapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mGameBargainAdapter.addData(data)
            } else {
                mGameBargainAdapter.setNewInstance(data)
            }
        })

        mViewModel.collectGoodsLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mMallCollectAdapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMallCollectAdapter.addData(data)
            } else {
                mMallCollectAdapter.setNewInstance(data)
            }
        })
    }

}